package ai4games_tests.tests;

import game2dai.Domain;
import game2dai.World;
import game2dai.entities.Vehicle;
import game2dai.entityshapes.Picture;
import game2dai.entityshapes.ps.ArrowPic;
import game2dai.entityshapes.ps.Hints;
import game2dai.maths.Vector2D;
import game2dai.steering.AutoPilot;
import game2dai.steering.SBF;
import processing.core.PApplet;

/**
 * Arrive, seek and flee demonstrator
 * @author Peter
 *
 */
public class GameTest_002 extends PApplet {


	long ct, et, lt;
	float deltaTime;
	
	World w;
	Domain wd;

	Vehicle v;
	Picture view;
	AutoPilot sbSeek, sbFlee, sbArrive;
	
	Vector2D target;
	
	public void setup(){
		size(600,600);
		w = new World(width, height);
		target = new Vector2D(width/2, height/2);
		v = new Vehicle(new Vector2D(20,40), // position
				6, // collision radius
				new Vector2D(80,0), // velocity
				50, // maximum speed
				new Vector2D(1,0), // heading
				1, // mass
				1,  // turning rate
				200 // max force
				); 
		view = new ArrowPic(this);		
		view.showHints(Hints.HINT_HEADING | Hints.HINT_VELOCITY);
		v.renderer(view);
		wd = new Domain(0, 0, width, height);
	
		sbSeek = new AutoPilot();
//		sbSeek.setTarget(SBF.SEEK, target);
		sbSeek.seekOn(target);  //enableBehaviours(SBF.SEEK);
		
		sbFlee = new AutoPilot();
//		sbFlee.setTarget(SBF.FLEE, target);
		sbFlee.fleeOn(target); // enableBehaviours(SBF.FLEE);
		
		sbArrive = new AutoPilot();
//		sbArrive.setTarget(SBF.ARRIVE, target);
//		sbArrive.enableBehaviours(SBF.ARRIVE);
		sbArrive.arriveOn(target);
		ct = lt = millis();
		ellipseMode(CENTER);
		
		w.add(v);
//		frameRate(4);
	}
	
	public void draw(){
		ct = millis();
		et = ct - lt;
		lt = ct;
		deltaTime = et / 1000.0f;
		background(128,255,128);
		fill(0,255,0);
		ellipse((float)target.x, (float)target.y, 10,10);
		
		stroke(0);
		fill(0);
		text("Select steering behaviour    [S]eek   [A]rrive   [F]lee", 10,20);
		w.update(deltaTime);
		w.draw();
	}
	
	public void keyPressed(){
		if(key == 's')
			v.AP(sbSeek);
		if(key == 'a')
			v.AP(sbArrive);
		if(key == 'f')
			v.AP(sbFlee);
	}
	
	
}

